package com.lagou.edu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * @author: create by CharwayH
 * @description: com.lagou.edu.utils
 * @date:2022/3/6
 * 事务管理类:便于事务的开启，提交，回滚
 */
@Component
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * 开启事务
     */
    public void beginTransaction() throws SQLException {
        // 关闭事务的自动提交
        connectionUtils.getCurrentConnection().setAutoCommit(false);
    }

    /**
     * 提交事务
     */
    public void commit() throws SQLException {
        connectionUtils.getCurrentConnection().commit();
    }

    /**
     * 回滚事务
     */
    public void rollback() throws SQLException {
        connectionUtils.getCurrentConnection().rollback();
    }
}